Note: This tutorial assumes that you have completed the previous tutorials: Arduino IDE Setup. |
Please ask about problems and questions regarding this tutorial on answers.ros.org. Don't forget to include in your question the link to this page, the versions of your OS & ROS, and also add appropriate tags. |
An Example rosserial_xbee Network
Description: A guide to building a simple rosserial_xbee networkTutorial Level: BEGINNER
The rosserial_xbee package is meant to simplify setting up a network of ROS hardware nodes throughout a building. This tutorial will show how to have two Arduino publishers on a rosserial_xbee network. It will walk you through purchasing hardware, programming your XBees, programming your Arduinos, and launching the network.
Hardware
A rosserial_xbee network needs a computer running ROS, an XBee connected to the computer, and rosserial hardware nodes with XBees. All Xbees used in a rosserial_xbee network must be XBee series 1. Series 1 and 2 have different binary APIs which make them incompatible. Arduinos were chosen as the hardware platform because as of the writing of this tutorial, they have the only complete implementation of rosserial. However, you can use any board with a serial port and rosserial implementation.
For this tutorial, you can purchase :
3 x XBee Series 1
1 x XBee Explorer
2 x XBee shield
2 x Header
2 x Arduino Uno
Plus, you may want to purchase a battery pack or power supply for your Arduino. These will allow you to set up your hardware nodes away from a computer.
Before you begin, you will need to solder header on to your XBee shield so that you can plug it into your Arduino.
Software
Configuring the XBees
The first step in setting up our network is setting up the XBees. Normally to set up the XBees you would use Digi's Windows only X-CTU software. To simplify deployment of rosserial_xbee networks we have included setup_xbee.py to program your xbee from Linux.
Plug your first Xbee into your Xbee Explorer and attach the Xbee explorer via USB. It should show up as /dev/ttyUSB*.
The first XBee that we will set up will be the coordinator for the network. It will be the XBee connected to the computer. Run
rosrun rosserial_xbee setup_xbee.py -C /dev/ttyUSB0 0
This will set up the XBee and set its ID to 0. All rosserial_xbee network coordinators should have an ID of 0. It also sets up some default configurations. The XBee now communicates using API mode 2 (a binary protocol with escape characters) at 57600 baud. It is also on the default network ID (1331) and frequency channel (D).
Its a good idea to physically mark the XBee with sharpie as C0 (coordinator with id). You are going to have several XBees and you need to keep track of how they are programmed.
Next, we are going to program the other two XBees. These XBees will be configured as end devices in our network. They will always send their data to the XBee with ID 0. Additionally, their communication interface is transparent. They communicate in API mode 0 at 56700 baud. They will be connected to our Arduinos.
XBee for node 1
rosrun rosserial_xbee setup_xbee.py /dev/ttyUSB0 1
XBee for node 2
rosrun rosserial_xbee setup_xbee.py /dev/ttyUSB0 2
Programing Your Arduino
Open up your Arduino IDE, copy the following code into your IDE, and then program your Node 1 Arduino with the code.
1 #include <ros.h>
2 #include <std_msgs/String.h>
3
4 ros::NodeHandle nh;
5
6 std_msgs::String str_msg;
7 ros::Publisher chatter("chatter", &str_msg);
8
9 unsigned char hello[] = "Hello from Node 1!";
10
11 void setup()
12 {
13 nh.initNode();
14 nh.advertise(chatter);
15 }
16
17 void loop()
18 {
19 str_msg.data = hello;
20 chatter.publish( &str_msg );
21 nh.spinOnce();
22 delay(1000);
23 }
Now program your Node 2 Arduino with the same code, but change the greeting from
Error: No code_block found to
1 unsigned char hello[] = "Hello from Node 2!";
Testing
Now its time to wireup our hardware nodes and connect the coordinator XBee to the computer using the XBee explorer.
Run roscore
roscore
In a new terminal launch the rosserial_xbee node and tell it the XBee Explorer's serial port and the id numbers of your node XBees.
rosrun rosserial_xbee xbee_network.py /dev/ttyUSB0 1 2
Now in a new terminal launch rostopic echo and listen to the chatter of your rosserial_xbee nodes!
rostopic echo chatter
Whats next
This tutorial went over the most basic hardware node. It is now time to add sensors and actuators to your nodes so that you can augment your ROS system. Now you can collect information anywhere it is produced instead of being tied to a robot or a computer. You can use temperature sensors, barometers, door sensors, switches, Ultrasound rangers, IR Rangers, PIR motion detectors, light sensors, etc. Demos of many of these sensors are available in rosserial_arduino_demos You also attach motors, servos, or linear actuators to your hardware nodes to augment your robots.